package sign_in.loginsystem.service.realize;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sign_in.loginsystem.mapper.RoleAndPemMapper;
import sign_in.loginsystem.model.entity.Menu;
import sign_in.loginsystem.model.util.PageBean;
import sign_in.loginsystem.model.util.RBody;
import sign_in.loginsystem.service.port.MenuServes;

import java.util.ArrayList;


@Service
public class MenuImp implements MenuServes {

    @Autowired
    RoleAndPemMapper menuMapper;

    @Override
    public PageBean allMenu(RBody body) {
        PageBean pageBean = body.getPageBean();
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Menu> page = (Page<Menu>) menuMapper.allMenu();
        pageBean.setAllPageNum((int) page.getTotal());
        pageBean.setMenus((ArrayList<Menu>) page.getResult());
        pageBean.setAllObjectNum(menuMapper.menuNum());
        if(menuMapper.menuNum()%pageBean.getPageSize()!=0){
            pageBean.setAllPageNum(menuMapper.menuNum()/pageBean.getPageSize()+1);
        }
        else {
            pageBean.setAllPageNum(menuMapper.menuNum()/pageBean.getPageSize());
        }
        return pageBean;
    }

    @Override
    public PageBean delMenu(RBody body) {
        menuMapper.delMenu(body.getMenu());
        return allMenu(body);
    }

    @Override
    public PageBean createMenu(RBody body) {
        menuMapper.createMenu(body.getMenu());
        return allMenu(body);
    }

    @Override
    public PageBean editMenu(RBody body) {
        menuMapper.editMenu(body.getMenu());
        return allMenu(body);
    }
}
